package com.lzy.system.organization.controller;

import com.lzy.conf.JSONResult;
import com.lzy.system.organization.dto.SysUserMappingAllocationDTO;
import com.lzy.system.organization.entity.SysRole;
import com.lzy.system.organization.entity.SysUserMapping;
import com.lzy.system.organization.service.ISysUserMappingService;
import com.lzy.utils.ValidList;
import com.lzy.validator.TypeConstraint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 用户映射 前端控制器
 * </p>
 *
 * @author lcc
 * @since 2023-11-25
 */
@RestController
@RequestMapping("/organization/sysUserMapping")
@Validated
@Api(tags = "用户映射")
public class SysUserMappingController {
    private ISysUserMappingService iSysUserMappingService;

    @Autowired
    public SysUserMappingController(ISysUserMappingService iSysUserMappingService) {
        this.iSysUserMappingService = iSysUserMappingService;
    }

    @ApiOperation("修改管理状态")
    @RequestMapping(value = "updateManageStatus", method = RequestMethod.POST)
    public JSONResult updateManageStatus(
            @ApiParam("id") @NotNull(message = "id不能为空") @RequestParam(name = "id") Long id,
            @ApiParam("管理状态") @TypeConstraint(values = {0, 1}, message = "是否管理必须是0或者1") @RequestParam(name = "is_manage", required = true) Integer is_manage
    ) throws Exception {
        SysUserMapping sysUserMapping = new SysUserMapping();
        sysUserMapping.setId(id);
        sysUserMapping.setIs_manage(is_manage);
        return JSONResult.ok(iSysUserMappingService.update(sysUserMapping));
    }

    @ApiOperation("修改默认身份")
    @RequestMapping(value = "updateDefStatus", method = RequestMethod.POST)
    public JSONResult updateDefStatus(
            @ApiParam("id") @NotNull(message = "id不能为空") @RequestParam(name = "id") Long id,
            @ApiParam("用户id") @NotNull(message = "用户id不能为空") @RequestParam(name = "user_id") Long user_id,
            @ApiParam("默认身份状态") @TypeConstraint(values = {0, 1}, message = "默认身份必须是0或者1") @RequestParam(name = "is_def") Integer is_def
    ) throws Exception {
        return JSONResult.ok(iSysUserMappingService.updateDef(user_id, id, is_def));
    }

    @ApiOperation("分配用户")
    @RequestMapping(value = "userAllocation", method = RequestMethod.POST)
    public JSONResult userAllocation(
            @ApiParam("用户分配信息") @RequestBody @Validated List<SysUserMappingAllocationDTO> sysUserMappingAllocationDTOList
    ) throws Exception {
        return JSONResult.ok(iSysUserMappingService.addUser(sysUserMappingAllocationDTOList));
    }

    @ApiOperation("删除分配用户")
    @RequestMapping(value = "delUser", method = RequestMethod.POST)
    public JSONResult delUser(
            @ApiParam("用户分配信息") @RequestBody @Validated List<SysUserMapping> sysUserMappings
    ) throws Exception {
        return JSONResult.ok(iSysUserMappingService.delUser(sysUserMappings));
    }

    @ApiOperation("查询用户列表")
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public JSONResult getList(
            @ApiParam("关键字") @RequestParam(name = "keyword", required = false) String keyword,
            @ApiParam("公司id") @RequestParam(name = "company_id", required = false) Long company_id,
            @ApiParam("部门id") @RequestParam(name = "dep_id", required = false) Long dep_id,
            @ApiParam("每页显示行数") @RequestParam(name = "limit", required = true, defaultValue = "10") int limit,
            @ApiParam("当前页数") @RequestParam(name = "page", required = true, defaultValue = "1") int page
    ) throws Exception {
        return JSONResult.ok(iSysUserMappingService.getList(keyword, company_id, dep_id, limit, page));
    }

    @ApiOperation("用户绑定角色")
    @RequestMapping(value = "savePowerRole/{mid}",method = RequestMethod.POST)
    public JSONResult savePower(
            @ApiParam("用户映射id") @PathVariable(name = "mid") Long mid,
            @ApiParam("角色列表") @RequestBody() @Validated ValidList<SysRole> sysRoles

    ) throws Exception{
        return JSONResult.ok(iSysUserMappingService.savePower(mid,sysRoles));
    }
}
